package com.ywgt.system.controller;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ywgt.response.Result;
import com.ywgt.system.pojo.Menu;
import com.ywgt.system.pojo.Role;
import com.ywgt.system.pojo.RoleMenu;
import com.ywgt.system.service.MenuService;
import com.ywgt.system.service.RoleMenuService;
import com.ywgt.system.service.RoleService;
import com.ywgt.system.service.UserService;
import com.ywgt.system.vo.RoleVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author yiping
 * @since 2021-01-18
 */
@RestController
@Api(value = "角色模块",tags = "角色模块")
@RequestMapping("/system/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private MenuService menuService;

    @Autowired
    private UserService userService;

    @Autowired
    private RoleMenuService roleMenuService;


    @ApiOperation(value = "获取角色列表",notes = "获取角色列表")
    @GetMapping("/findRoleList")
    public Result getRoles(@RequestParam(value = "pageNum",defaultValue = "1")Long pageNum, @RequestParam(value = "pageSize",defaultValue = "8")Long pageSize,
                           RoleVO roleVO){
        Page<Role> rolePage=roleService.findRoles(pageNum,pageSize,roleVO);
        List<Role> records = rolePage.getRecords();
        long total = rolePage.getTotal();
        return Result.ok().data("rows",records).data("total",total);
    }
    @ApiOperation(value = "添加角色",notes = "添加角色")
    @PostMapping("/add")
    public Result addRole(@RequestBody Role role){
        if (roleService.save(role)){
            return Result.ok();
        }
        return Result.error();
    }
    @ApiOperation(value = "获取角色",notes = "获取角色信息")
    @GetMapping("/edit/{id}")
    public Result update(@PathVariable("id")Long id){
        Role role = roleService.getById(id);
        Map<String,Object> info=new HashMap<>();
        info.put("roleName",role.getRoleName());
        info.put("remark",role.getRemark());
        return Result.ok().data("data",role);
    }
    @ApiOperation(value = "更新角色角色",notes = "更新角色")
    @PutMapping("/update/{id}")
    public Result update(@PathVariable("id")Long id,@RequestBody Map<String,Object> info){
        String roleName = (String) info.get("roleName");
        String remark = (String) info.get("remark");
        Role role=new Role();
        role.setId(id);
        role.setRoleName(roleName);
        role.setRemark(remark);
        if (roleService.updateById(role)){
            return Result.ok();
        }
        return Result.error();
    }
    @ApiOperation(value = "删除角色",notes = "根据id删除角色")
    @DeleteMapping("/delete/{id}")
    public Result deleteRole(@PathVariable("id")Long id){
        if (roleService.removeById(id)){
            return Result.ok();
        }
        return Result.error();
    }
    @ApiOperation(value = "修改角色状态",notes = "修改角色状态")
    @PutMapping("/updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable("id")Long id,@PathVariable("status")Integer status){
        LambdaUpdateWrapper<Role> updateWrapper=new LambdaUpdateWrapper<>();
        Role role=new Role();
        role.setStatus(status==0?1:0);
        updateWrapper.eq(Role::getId,id);
        if (roleService.update(role,updateWrapper)){
            return Result.ok();
        }
        return Result.error();
    }
    @ApiOperation(value = "获取角色菜单",notes = "获取角色菜单")
    @GetMapping("/findRoleMenu/{id}")
    public Result findRoleMenu(@PathVariable("id")Integer id){
        List<Menu> tree = menuService.getMenuTree();
        Role role = roleService.getById(id);
        List<Menu> menus=menuService.getMenuByRoleId(id);
        List<Long> ids=new ArrayList<>();
        for (Menu menu : menus) {
            ids.add(menu.getId());
        }
        return Result.ok().data("tree",tree).data("open",ids);
    }
    @ApiOperation(value = "添加角色",notes = "添加角色")
    @PostMapping("/authority/{id}")
    public Result authority(@PathVariable("id")Long id, @RequestBody Long[] ids){
        Collection<RoleMenu> roleMenus=new ArrayList<>();
        for (int i = 0; i < ids.length; i++) {
            RoleMenu roleMenu=new RoleMenu();
            roleMenu.setRoleId(id);
            roleMenu.setMenuId(ids[i]);
            roleMenus.add(roleMenu);
        }
        if (roleMenuService.saveBatch(roleMenus)){
            return Result.ok();
        }
        return Result.error();
    }


}

